package com.lvwenguo.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lvwenguo.user.entity.User;
import com.lvwenguo.user.dao.UserDao;
import com.lvwenguo.user.service.UserService;
import org.assertj.core.internal.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Autowired
    UserDao userDao;

    @Override
    public List<User> selectUser() {
        //return userDao.selectUser();
        return this.list();
    }

    @Override
    public User login(String name, String password) {
        User user = this.getOne(new LambdaQueryWrapper<User>().eq(User::getUserName, name).eq(User::getUserPassword, password));
        return user;
    }

    @Override
    public List<User> selectUser(String name) {
        return baseMapper.selectList(new LambdaQueryWrapper<User>().eq(User::getUserName, name));
    }

    @Override
    public Map<Integer,String> saveUser(User user) {
        Map<Integer,String> map = new HashMap<>();
        User user1 = this.baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUserName, user.getUserName()).or().eq(User::getUserMobilePhone, user.getUserMobilePhone()));
        if (user1 != null) {
            if (user.getUserName().equals(user1.getUserName())) {
                map.put(1,"用户名已注册请重新输入！");

            } else {
                map.put(1,"改手机号已注册！");
            }

        } else {

            this.baseMapper.insert(user);
            map.put(2,"注册成功，赶快登录吧~~!");

        }
        return map;
    }
}
